package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @PostMapping("login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session){

        String password =employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee>  queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one=employeeService.getOne(queryWrapper);
        if (one==null){
            return R.error("登录失败");
        }
        if (!one.getPassword().equals(password)){
            return R.error("登录失败");
        }
        if ((one.getStatus()==0)){
            return R.error("登录失败");
        }
        session.setAttribute("employee",one.getId());
        return R.success(one);
    }
    @PostMapping("/logout")
    public R<String>logout(HttpSession session){
        //session.removeAttribute("employee");
        session.invalidate();
        return R.success("退出成功");
    }
    @PostMapping
    public R<String> save(@RequestBody Employee employee){
        String password=DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        Long empId=(Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("添加员工成功");
    }
    @GetMapping("/page")
    public R<Page<Employee>> page(Integer page,Integer pageSize,String name){
        Page<Employee> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        queryWrapper.orderByAsc(Employee::getUpdateTime);
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpSession session){
        if(employee.getId()==1){
            return R.error("管理员不允许禁用");
        }
//        employee.setUpdateTime((LocalDateTime.now()) );
//        employee.setUpdateUser((Long) session.getAttribute("employee"));
        employeeService.updateById(employee);
        return R.success("启用禁用成功");
    }

    /**
     * 请求方式是get
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getEmployeeById(@PathVariable Long id) {
        log.info("编辑回显");
        Employee employee = employeeService.getById(id);
        if(employee!=null){
            return R.success(employee);
        }
        return R.error("没有找到用户");
    }
}